#!/usr/bin/perl
#

open(MOR,$ARGV[0]);
while($morLine=<MOR>) {
	chomp($morLine);
	($num,$word,$morph) = split(/\t+/,$morLine);
	@morphEntries = split(/DELIM/,$morph);
	$entryNum=0;
	foreach $entry (@morphEntries) {
		$entry=~/<fs af='([^']+)'>/;
		@fields=split(/,/,$1);
		$myMorph{$word}->[$entryNum]->{'root'} = $fields[0];
		$myMorph{$word}->[$entryNum]->{'cat'} = $fields[1];
		$myMorph{$word}->[$entryNum]->{'gen'} = $fields[2];
		$myMorph{$word}->[$entryNum]->{'num'} = $fields[3];
		$myMorph{$word}->[$entryNum]->{'per'} = $fields[4];
		$myMorph{$word}->[$entryNum]->{'obl'} = $fields[5];
		$myMorph{$word}->[$entryNum]->{'tam'} = $fields[7];
		$entryNum++;
	}
}
close(MOR);

open(COMPAT,$ARGV[1]);
while($compat=<COMPAT>) {
	chomp($compat);
	($oldPos,$newPos) = split(/\s+/,$compat);
	$compat{"$oldPos $newPos"} = 1;
}
close(COMPAT);


while($line=<STDIN>) {
	chomp($line);
	if($line=~/^\s*$/) {
		print "\n";
		next;
	}
	@words = split(/\s+/,$line);

	$sentence = "";
	foreach $w (@words) {
		($word,$pos) = split(/_/,$w);

		my $correctEntry = "";
		for($j=0;$j<(@{$myMorph{$word}});$j++) {
			$entry = $myMorph{$word}->[$j];
			if($compat{"$pos $entry->{'cat'}"}) {
				$correctEntry = $entry;
				last;
			}
		}

		if($myMorph{$word}) {
			if(!$correctEntry) {
				$correctEntry = $myMorph{$word}->[0];
			}

			$sentence.=$word."_".$pos."_".$correctEntry->{'root'}."DELIM".$correctEntry->{'gen'}."DELIM".$correctEntry->{'num'}."DELIM".$correctEntry->{'per'}." ";
		}
		else {
			$sentence.=$word."_".$pos."_".$word."DELIMmDELIMsgDELIM3 ";
		}

	}
	$sentence=~s/ $//g;
	print $sentence."\n";
}


